﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Protocol
{
    public class ResInputMessage
    {
        public int LogicFrame;
        public List<InputCommand> Cmds = new List<InputCommand>();
        public byte[] Encode()
        {
            byte[] buffer = null;
            using (MemoryStreamExt ms = new MemoryStreamExt())
            {
                ms.WriteInt(LogicFrame);
                ms.WriteInt(Cmds.Count);
                for (int i = 0; i < Cmds.Count; i++)
                {
                    ms.WriteInt(Cmds[i].PlayerId);
                    ms.WriteFloat(Cmds[i].XAxis);
                    ms.WriteFloat(Cmds[i].YAxis);
                    ms.WriteByte((byte)Cmds[i].InputType);
                    ms.WriteByte((byte)Cmds[i].InputStatus);
                }
                buffer = ms.ToArray();
            }
            return buffer;
        }

        public static ResInputMessage Decode(byte[] buffer)
        {
            ResInputMessage m = new ResInputMessage();
            using (MemoryStreamExt ms = new MemoryStreamExt(buffer))
            {
                m.LogicFrame = ms.ReadInt();
                int count = ms.ReadInt();
                m.Cmds = new List<InputCommand>();
                for (int i = 0; i < count; i++)
                {
                    InputCommand c = new InputCommand();
                    c.PlayerId = ms.ReadInt();
                    c.XAxis = ms.ReadFloat();
                    c.YAxis = ms.ReadFloat();
                    c.InputType = (EInputType)ms.ReadByte();
                    c.InputStatus = (EInputStatus)ms.ReadByte();
                    m.Cmds.Add(c);
                }
            }
            return m;
        }
    }
}
